package com.daxt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.archives.ArchivesDocExtMapper;
import com.daxt.mapper.archives.ArchivesDossierExtMapper;
import com.daxt.mapper.sys.ArchivesTypeMapper;
import com.daxt.model.service.archives.param.AddCol;
import com.daxt.model.service.archives.result.ArchivesTypeCols;
import com.daxt.model.service.archives.vo.ArchivesDocExt;
import com.daxt.model.service.archives.vo.ArchivesDossierExt;
import com.daxt.model.service.archives.vo.ArchivesType;
import com.daxt.service.ArchivesFondsService;
import com.daxt.utils.CreateIndexUtil;


@Service
public class ArchivesFondsServiceImpl implements ArchivesFondsService{
	
	@Autowired
	private ArchivesTypeMapper archivesTypeMapper;
	@Autowired
	private ArchivesDocExtMapper archivesDocExtMapper;
	@Autowired
	private ArchivesDossierExtMapper archivesDossierExtMapper;
	

	@Override
	public Result<ArchivesTypeCols> getArchivesTypeCols(String id) {
		
	/*	ArchivesType archivesType = archivesTypeMapper.selectById(id);
		if(archivesType==null) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"档案类型不存在", null);
		}
		ArchivesTypeCols cols=new ArchivesTypeCols();
		cols.setArchivesType(archivesType);
		if("0".equals(archivesType.getModel())){
			QueryWrapper<Warehouse> queryWrapperDoc=new QueryWrapper<Warehouse>();
			queryWrapperDoc.eq("extType", "doc");
			List<Warehouse> docList = archivesBaseMapper.selectList(queryWrapperDoc);
			cols.setDocBaseList(docList==null?new ArrayList<Warehouse>():docList);
			QueryWrapper<ArchivesDocExt> queryWrapperDocExt=new QueryWrapper<ArchivesDocExt>();
			queryWrapperDocExt.eq("extType", archivesType.getTypeNum());
			queryWrapperDocExt.eq("fileType", "doc");
			List<ArchivesDocExt> docExtList = archivesDocExtMapper.selectList(queryWrapperDocExt);
			cols.setDocList(docExtList==null?new ArrayList<ArchivesDocExt>():docExtList);
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),"", cols);
		}else if("1".equals(archivesType.getModel())) {
			QueryWrapper<Warehouse> queryWrapperDossier=new QueryWrapper<Warehouse>();
			queryWrapperDossier.eq("extType", "dossier");
			List<Warehouse> dossierList = archivesBaseMapper.selectList(queryWrapperDossier);
			cols.setDossierBaseList(dossierList==null?new ArrayList<Warehouse>():dossierList);
			QueryWrapper<Warehouse> queryWrapperDoc=new QueryWrapper<Warehouse>();
			queryWrapperDoc.eq("extType", "doc");
			List<Warehouse> docList = archivesBaseMapper.selectList(queryWrapperDoc);
			cols.setDocBaseList(docList==null?new ArrayList<Warehouse>():docList);
			QueryWrapper<ArchivesDocExt> queryWrapperDocExt=new QueryWrapper<ArchivesDocExt>();
			queryWrapperDocExt.eq("extType", archivesType.getTypeNum());
			queryWrapperDocExt.eq("fileType", "doc");
			List<ArchivesDocExt> docExtList = archivesDocExtMapper.selectList(queryWrapperDocExt);
			cols.setDocList(docExtList==null?new ArrayList<ArchivesDocExt>():docExtList);
			QueryWrapper<ArchivesDocExt> queryWrapperDossierExt=new QueryWrapper<ArchivesDocExt>();
			queryWrapperDossierExt.eq("extType", archivesType.getTypeNum());
			queryWrapperDossierExt.eq("fileType", "dossier");
			List<ArchivesDocExt> dossierExtList = archivesDocExtMapper.selectList(queryWrapperDossierExt);
			cols.setDossierList(dossierExtList==null?new ArrayList<ArchivesDocExt>():dossierExtList);
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),"", cols);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"档案类型错误", null);
		}*/
		return null;
		
	}

	@Override
	public Result<String> setCol(String id, String type, AddCol addCol) {
		ArchivesType archivesType = archivesTypeMapper.selectById(id);
		if(archivesType==null) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类型错误", "");
		}
		String extColumn="";
		try {
		if("doc".equals(type)) {
			QueryWrapper<ArchivesDocExt> queryWrapperCol=new QueryWrapper<ArchivesDocExt>();
			queryWrapperCol.select("extColumn");
			queryWrapperCol.orderByDesc("extColumn");
			IPage<ArchivesDocExt> page=new Page<ArchivesDocExt>(1,1);
			IPage<ArchivesDocExt> extColumnTop = archivesDocExtMapper.selectPage(page, queryWrapperCol);
			if(extColumnTop.getRecords().size()>0) 
				extColumn = extColumnTop.getRecords().get(0).getExtColumn();
			ArchivesDocExt ext=new ArchivesDocExt();
			ext.setExtColumn(CreateIndexUtil.createIndex(extColumn));
			ext.setColumnComment(addCol.getColumnComment());
			ext.setIsEdit(addCol.getIsEdit());
			ext.setIsList(addCol.getIsList());
			ext.setIsQuery(addCol.getIsQuery());
			ext.setExtType(archivesType.getTypeNum());
			ext.setFondsNum(archivesType.getFondsNum());
			ext.setJavaType("String");
			ext.setFileType("doc");
			archivesDocExtMapper.insert(ext);
		}else if("dossier".equals(type)) {
			QueryWrapper<ArchivesDocExt> queryWrapperCol1=new QueryWrapper<>();
			queryWrapperCol1.select("id","extColumn");
			queryWrapperCol1.orderByDesc("extColumn");
			IPage<ArchivesDocExt> page1=new Page<>(1,1);
			IPage<ArchivesDocExt> extColumnTop1 = archivesDocExtMapper.selectPage(page1, queryWrapperCol1);
			if(extColumnTop1.getRecords().size()>0) 
				extColumn = extColumnTop1.getRecords().get(0).getExtColumn();
			ArchivesDocExt ext=new ArchivesDocExt();
			ext.setExtColumn(CreateIndexUtil.createIndex(extColumn));
			ext.setColumnComment(addCol.getColumnComment());
			ext.setIsEdit(addCol.getIsEdit());
			ext.setIsList(addCol.getIsList());
			ext.setIsQuery(addCol.getIsQuery());
			ext.setExtType(archivesType.getTypeNum());
			ext.setFondsNum(archivesType.getFondsNum());
			ext.setJavaType("String");
			ext.setFileType("dossier");
			ext.setSort(0);
			archivesDocExtMapper.insert(ext);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类型错误", "");
		}
		}catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "添加失败", "");
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", "");
	}

	@Override
	public Result<String> editCol(String id, String type, AddCol addCol) {
		ArchivesType archivesType = archivesTypeMapper.selectById(id);
		if(archivesType==null) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类型错误", "");
		}
		if("doc".equals(type)) {
			ArchivesDocExt ext=new ArchivesDocExt();
			ext.setId(Long.parseLong(addCol.getId()));
			ext.setColumnComment(addCol.getColumnComment());
			ext.setIsEdit(addCol.getIsEdit());
			ext.setIsList(addCol.getIsList());
			ext.setIsQuery(addCol.getIsQuery());
			//ext.setExtType(archivesType.getTypeNum());
			archivesDocExtMapper.updateById(ext);
		}else if("dossier".equals(type)) {
			ArchivesDossierExt ext=new ArchivesDossierExt();
			ext.setId(Long.parseLong(addCol.getId()));
			ext.setColumnComment(addCol.getColumnComment());
			ext.setIsEdit(addCol.getIsEdit());
			ext.setIsList(addCol.getIsList());
			ext.setIsQuery(addCol.getIsQuery());
			//ext.setExtType(archivesType.getTypeNum());
			archivesDossierExtMapper.updateById(ext);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类型错误", "");
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", "");
	}
	
	
}
